package org.slf4j.impl;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

public class XtraTinyLoggerConfigurator {
	private static final String CONFIG_FILENAME = "XtraTinyLogger.config";

	private static final String FILE_APPENDER_NAME = "FileAppender";
	private static final String CONSOLE_APPENDER_NAME = "ConsoleAppender";

	private static final String APPENDERS_FORMAT = "logger.%s.appenders";
	private static final String LEVEL_FORMAT = "logger.%s.level";
	private static final String FORMAT_FORMAT = "logger.%s.format";

	private XtraTinyLoggerConsoleAppender consoleAppender = null;
	private XtraTinyLoggerFileAppender fileAppender = null;

	private Properties properties;

	public XtraTinyLoggerConfigurator() {
		String value = System.getProperty(CONFIG_FILENAME);
		properties = new Properties();
		if (value != null) {
			try {
				FileInputStream fis = new FileInputStream(value);
				properties.load(fis);
				fis.close();
			} catch (IOException e) {
				System.err
						.println("Failed to load logger configuration from file.");
			}
		}
	}

	public Set<XtraTinyLoggerAppender> getAppenders(String name) {
		Set<XtraTinyLoggerAppender> appenders = new HashSet<XtraTinyLoggerAppender>();
		fillAppenders(appenders, name);
		if (appenders.size() == 0) {
			appenders.add(getDefaultAppender());
		}
		return appenders;
	}

	private void fillAppenders(Set<XtraTinyLoggerAppender> appenders,
			String name) {
		String prop = String.format(APPENDERS_FORMAT, name);
		String value = properties.getProperty(prop);
		if (value != null) {
			String[] pieces = value.split(",");
			for (String appenderName : pieces) {
				XtraTinyLoggerAppender appender = getAppenderByName(appenderName);
				if (appender != null) {
					appenders.add(appender);
				}
			}
		}
	}

	private XtraTinyLoggerAppender getAppenderByName(String appenderName) {
		if (appenderName.equals(CONSOLE_APPENDER_NAME)) {
			return getConsoleAppender();
		} else if (appenderName.equals(FILE_APPENDER_NAME)) {
			return getFileAppender();
		} else {
			return null;
		}
	}

	private XtraTinyLoggerAppender getConsoleAppender() {
		if (consoleAppender == null) {
			consoleAppender = new XtraTinyLoggerConsoleAppender();
		}
		return consoleAppender;
	}

	private XtraTinyLoggerAppender getFileAppender() {
		if (fileAppender == null) {
			try {
				fileAppender = new XtraTinyLoggerFileAppender();
			} catch (XtraTinyLoggerInstantiationException e) {
				System.err.println(e.getMessage());
				fileAppender = null;
			}
		}
		return fileAppender;
	}

	private XtraTinyLoggerAppender getDefaultAppender() {
		return getConsoleAppender();
	}

	public LoggingLevel getLoggerLevel(String name) {
		String prop = String.format(LEVEL_FORMAT, name);
		String value = properties.getProperty(prop);

		if (value == null) {
			return null;
		}

		switch (value) {
		case "ALL":
			return LoggingLevel.ALL;
		case "INFO":
			return LoggingLevel.INFO;
		case "DEBUG":
			return LoggingLevel.DEBUG;
		case "ERROR":
			return LoggingLevel.ERROR;
		default:
			return null;
		}
	}

	public String getLoggerFormat(String name) {
		String prop = String.format(FORMAT_FORMAT, name);
		String value = properties.getProperty(prop);

		if (value == null) {
			return null;
		}

		return value;
	}
}
